/*
 * pps_bom
 */
package com.pitop.bom.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.bom.dao.BomDetailLogDAO;
import com.pitop.bom.entity.BomDetailLog;
import com.pitop.bom.entity.BomDetailLogState;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.BomDetailLogException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("bomDetailLogSV")
@Slf4j
public class BomDetailLogSVImpl extends BaseSVImpl<BomDetailLog, Long> implements BomDetailLogSV {

    @Autowired
    private BomDetailLogDAO bomDetailLogDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return bomDetailLogDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(BomDetailLog entity) throws BaseException {
        entity.setCreateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象BomDetailLog
     *
     * @param id
     * @return BomDetailLog
     */
    @Override
    public BomDetailLog load(Long id) {
        if (id == null) {
            throw new BomDetailLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return bomDetailLogDAO.load(param);
    }

    /**
     * 加载一个对象BomDetailLog,(将查询关联数据)
     *
     * @param id
     * @return BomDetailLog
     */
    @Override
    public BomDetailLog get(Long id) {
        if (id == null) {
            throw new BomDetailLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return bomDetailLogDAO.getDetail(id);
    }

    /**
     * 加载一个对象BomDetailLog 通过id
     *
     * @param id
     * @return BomDetailLog
     */
    @Override
    public BomDetailLog loadById(Long id) {
        if (id == null) {
            throw new BomDetailLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomDetailLogDAO.loadById(id);
    }

    /**
     * 加载一个对象BomDetailLog 通过id,(将查询关联数据)
     *
     * @param id
     * @return BomDetailLog
     */
    @Override
    public BomDetailLog getById(Long id) {
        if (id == null) {
            throw new BomDetailLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomDetailLogDAO.getById(id);
    }

    /**
     * 根据主键id,oldStates 共同更新 BomDetailLog 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, BomDetailLogState newState, BomDetailLogState... oldStates) {
        if (id == null) {
            throw new BomDetailLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new BomDetailLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new BomDetailLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        bomDetailLogDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 BomDetailLog 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, BomDetailLogState state) {
        if (id == null) {
            throw new BomDetailLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        bomDetailLogDAO.updateById(id, state.name(), new Date());
    }


    /**
     * 删除对象BomDetailLog
     *
     * @param id
     * @return BomDetailLog
     */
    @Override
    public void delete(Long id) {
        if (id == null) {
            throw new BomDetailLogException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        bomDetailLogDAO.delete(param);
    }


    /**
     * 查询BomDetailLog分页
     *
     * @param bomDetailLog 对象
     * @param offset       查询开始行
     * @param limit        查询行数
     * @return List<BomDetailLog>
     */
    @Override
    public List<BomDetailLog> list(BomDetailLog bomDetailLog, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (bomDetailLog != null) {
            map = JSON.parseObject(JSON.toJSONString(bomDetailLog, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return bomDetailLogDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(BomDetailLog bomDetailLog) {
        Map<String, Object> map = null;
        if (bomDetailLog != null) {
            map = JSON.parseObject(JSON.toJSONString(bomDetailLog, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return bomDetailLogDAO.count(map);
    }

    /**
     * 查询BomDetailLog分页
     *
     * @param id
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<BomDetailLog>
     */
    @Override
    public List<BomDetailLog> list(Long id, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return bomDetailLogDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return bomDetailLogDAO.count(param);
    }

}
